package br.ufg.apkandroidufg.dados.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.ufg.apkandroidufg.dados.DatabaseHelper;
import br.ufg.apkwsentidades.Materia;
import br.ufg.apkwsentidades.Turma;

public class TurmaDAO {

	private static TurmaDAO instance;
	private SQLiteDatabase database;

	public static TurmaDAO getInstance(Context context) {
		if (instance == null) {
			instance = new TurmaDAO(context);
		}
		return instance;
	}

	private TurmaDAO(Context context) {
		DatabaseHelper helper = new DatabaseHelper(context);
		database = helper.getWritableDatabase();
	}
	
	public List<Turma> buscarPorId(List<Integer> itensIds) {
		Cursor result = database.rawQuery(
				" SELECT  m.* FROM turma t WHERE t.id IN ("
						+ getIdsList(itensIds) + ") ", null);

		List<Turma> turmas = new ArrayList<Turma>();

		while (result.moveToNext()) {
			Integer idTurma = result.getInt(0);
			String nome = result.getString(1);

			turmas.add(new Turma(idTurma, nome));
		}

		result.close();

		return turmas;
	}

	private String getIdsList(List<Integer> itensIds) {
		StringBuilder s = new StringBuilder();
		for (int indice = 1; indice <= itensIds.size(); indice++) {
			s.append(itensIds.get(indice - 1));
			if (indice < itensIds.size()) {
				s.append(", ");
			}
		}
		return s.toString();
	}
}
